# coding: utf-8
from django.db import models
from lib.common import BasicManager
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger


class ModelManagerHotWords(BasicManager):
    # 获取vip列表
    def get_words_list(self, page, is_show=None, name=None):
        result = self.values("name", "id", "is_show").order_by("-sort")
        if is_show:
            result = result.filter(is_show=is_show)
        if name:
            result = result.filter(name=name)
        if not page:
            return {'retcode': 0, 'retlist': list(result)}
        paginator = Paginator(result, 15)
        try:
            result_page = paginator.page(page)
        except PageNotAnInteger:
            result_page = paginator.page(1)
        except EmptyPage as e:
            return {'retlist': [], 'curpageidx': page,
                    'hasnext': False, 'hasprevious': False}
        return {'retlist': list(result_page), 'curpageidx': page,
                'hasnext': result_page.has_next(), 'hasprevious': result_page.has_previous(),
                'total_page': paginator.num_pages, 'retcode': 0}


class HotWords(models.Model):
    sort = models.IntegerField("排序", default=0)
    name = models.CharField("名称", max_length=50)
    is_show = models.PositiveSmallIntegerField("是否显示", default=0)
    created_at = models.DateTimeField("添加时间", auto_now_add=True)
    updated_at = models.DateTimeField("修改时间", auto_now=True)

    objects = ModelManagerHotWords()

    class Meta:
        verbose_name = '热词'
        verbose_name_plural = verbose_name
        db_table = "momself_hotwords"

    def __str__(self):
        return self.name


mm_HotWords = HotWords.objects
